home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / src+obj / image.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  40.9 KB  |  1,639 lines

  1. /* cat > headers/image.h << "EOF" */
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  3. /* image.h: header for image.c file.            */
  4. /*        image.c contains routines of image file    */
  5. /*        operations: image load, save, zoom        */
  6. /*        (expansion, interpolation).            */
  7. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  8. /* SCCS information: %W%    %G% - NCSA */
  9.  
  10. #define    image_h        1
  11.  
  12. #include "all.h"
  13. #include "newext.h"
  14.  
  15.     static int iformat;
  16.  
  17. /* EOF */
  18. /* cat > src+obj/image/check_image.c << "EOF" */
  19. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  20. /* check_image: tries to find a target image or data    */
  21. /*        and returns success or failure.    Success */
  22. /*        returns some useful information for    */
  23. /*        loading.                */
  24. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  25. /* SCCS information: %W%    %G% - NCSA */
  26.  
  27. /* #include "image.h" */
  28.  
  29. int
  30. check_image (in_type, fn, pout_type, pdim_x, pdim_y, piref, pload_pal)
  31.                  /* returns: 0 - success
  32.                          1 - filename not found
  33.                          2 - raw file - bad dimension
  34.                          value(s)
  35.                          3 - raw file - size is wrong
  36.                          4 - no RIS8
  37.                          5 - no SDS-2D
  38.                          6 - no RIS8 or SDS-2D in HDF file
  39.                          7 - size too big
  40.                          < 0 - DFerror - other HDF error */
  41.     enum desired_img_type in_type;
  42.                 /* input: desired image type */
  43.     char           *fn;
  44.                 /* input: filename to get image or data set */
  45.     enum img_type  *pout_type;
  46.                 /* returns: on success the actual image type */
  47.     int            *pdim_x;
  48.                 /* returns: on success the x dimension of the image or data set */
  49.     int            *pdim_y;
  50.                 /* returns: on success the y dimension of the image or data set */
  51.     uint16           *piref;
  52.                 /* returns: reference number of rig if RIS8 found */
  53.     bool           *pload_pal;
  54.                 /* input: if TRUE then attempt to load palette
  55.                       if RIS8 found.
  56.                    returns: set to false if TRUE on input
  57.                         and RIS8 found but has no palette. */
  58. {
  59.     DF             *file;
  60.     int             dummy, rank;
  61.     struct stat     file_stat;
  62.     uint16          tag, ref;
  63.     DFdesc          dd;
  64.     DFGRrig         rig;
  65.     DFSsdg          sdg;
  66.     int             dims[MAXSDSRANK];
  67.  
  68.     sdg.dimsizes = NULL;
  69.     sdg.coordsys = NULL;
  70.     sdg.dataluf[0] = NULL;
  71.     sdg.dataluf[1] = NULL;
  72.     sdg.dataluf[2] = NULL;
  73.     sdg.dimluf[0] = NULL;
  74.     sdg.dimluf[1] = NULL;
  75.     sdg.dimluf[2] = NULL;
  76.     sdg.dimscales = NULL;
  77.  
  78.     if ((file = DFopen (fn, DFACC_READ, dummy)) == NULL)
  79.     {        /* error */
  80.         switch (DFerror)
  81.         {
  82.             case DFE_FNF:    /* file not found */
  83.                 return (1);
  84.             case DFE_NOTDFFILE:    /* raw file */
  85.                 switch (in_type)
  86.                 {
  87.                     case FIRST:    /* check dimensions */
  88.                         if (get_imagesize (pdim_x, pdim_y))    /* bad dimensions */
  89.                             return (2);
  90.                         stat (fn, &file_stat);
  91.                         if (file_stat.st_size != (*pdim_x) * (*pdim_y))
  92.                             return (3);
  93.                         if (!size_ok (*pdim_x, *pdim_y))
  94.                             return (7);
  95.                         *pout_type = RAWI;
  96.                         return (0);
  97.                     case HDFR8:
  98.                         return (4);
  99.                     case HDFS2:
  100.                         return (5);
  101.                 }
  102.             default:
  103.                 return (DFerror);
  104.         }
  105.     }
  106.         /* try to verify the desired image or data in the HDF file */
  107.     switch (in_type)
  108.     {
  109.         case FIRST:    /* check for whatever comes first: RIS8 or SDS2D */
  110.             tag = ref = DFTAG_WILDCARD;
  111.             DFR8restart();
  112.             DFSDrestart();
  113.             if (DFsetfind (file, tag, ref))
  114.             {
  115.                 DFclose (file);
  116.                 return (DFerror);
  117.             }
  118.             while (TRUE)
  119.             {
  120.                 if (DFfind (file, &dd))
  121.                 {
  122.                     DFclose (file);
  123.                     return (6);
  124.                 }
  125.                 switch (dd.tag)
  126.                 {
  127.                     case DFTAG_RIG:
  128.                         if (DFGRgetrig (file, dd.ref, &rig))
  129.                         {
  130.                             DFclose (file);
  131.                             return (DFerror);
  132.                         }
  133.                         if (rig.datadesc[IMAGE].ncomponents != 1)
  134.                             break;
  135.                         *pdim_x = rig.datadesc[IMAGE].xdim;
  136.                         *pdim_y = rig.datadesc[IMAGE].ydim;
  137.                         *pout_type = RIS8;
  138.                         *piref = dd.ref;
  139.                         if (!rig.data[LUT].tag && *pload_pal)
  140.                             *pload_pal = FALSE;
  141.                         DFclose (file);
  142.                         if (!size_ok (*pdim_x, *pdim_y))
  143.                             return (7);
  144.                         return (0);
  145.                     case DFTAG_SDG:
  146.                         if (DFSDgetsdg (file, dd.ref, &sdg))
  147.                         {
  148.                             DFclose (file);
  149.                             return (DFerror);
  150.                         }
  151.                         if (sdg.rank != 2)
  152.                             break;
  153.                         *pdim_x = sdg.dimsizes[0];
  154.                         *pdim_y = sdg.dimsizes[1];
  155.                         *pout_type = SDS2D;
  156.                         DFclose (file);
  157.                         if (!size_ok (*pdim_x, *pdim_y))
  158.                             return (7);
  159.                         return (0);
  160.                     default:
  161.                         break;
  162.                 }
  163.             }
  164.         case HDFR8:    /* check for first RIS8 */
  165.             tag = DFTAG_RIG;
  166.             ref = DFTAG_WILDCARD;
  167.             DFR8restart();
  168.             if (DFsetfind (file, tag, ref))
  169.             {
  170.                 DFclose (file);
  171.                 return (DFerror);
  172.             }
  173.             while (TRUE)
  174.             {
  175.                 if (DFfind (file, &dd))
  176.                 {
  177.                     DFclose (file);
  178.                     return (4);
  179.                 }
  180.                 if (DFGRgetrig (file, dd.ref, &rig))
  181.                 {
  182.                     DFclose (file);
  183.                     return (DFerror);
  184.                 }
  185.                 if (rig.datadesc[IMAGE].ncomponents != 1)
  186.                     break;
  187.                 *pdim_x = rig.datadesc[IMAGE].xdim;
  188.                 *pdim_y = rig.datadesc[IMAGE].ydim;
  189.                 *pout_type = RIS8;
  190.                 *piref = dd.ref;
  191.                 if (!rig.data[LUT].tag && *pload_pal)
  192.                     *pload_pal = FALSE;
  193.                 DFclose (file);
  194.                 if (!size_ok (*pdim_x, *pdim_y))
  195.                     return (7);
  196.                 return (0);
  197.             }
  198.         case HDFS2:    /* check for first SDS2D */
  199.             DFclose (file);
  200.             DFSDrestart ();
  201.             while (TRUE)
  202.             {
  203.                 if (DFSDgetdims (fn, &rank, dims, MAXSDSRANK))
  204.                     switch (DFerror)
  205.                     {
  206.                         case DFE_NOMATCH:    /* not SDS-2D in file */
  207.                             return (4);
  208.                         case DFE_NOTENOUGH:    /* rank too big */
  209.                             break;
  210.                         default:
  211.                             return (DFerror);
  212.                     }
  213.                 else if (rank == 2)
  214.                 {        /* 2D dataset found */
  215.                     *pdim_x = dims[0];
  216.                     *pdim_y = dims[1];
  217.                     *pout_type = SDS2D;
  218.                     if (!size_ok (*pdim_x, *pdim_y))
  219.                         return (7);
  220.                     return (0);
  221.                 }
  222.             }
  223.     }
  224. }
  225. /* EOF */
  226. /* cat > src+obj/image/check_imgfn.c << "EOF" */
  227. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  228. /* check_imgfn: resolve image filename.            */
  229. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  230. /* SCCS information: %W%    %G% - NCSA */
  231.  
  232. /* #include "image.h" */
  233.  
  234. int 
  235. check_imgfn (imgfile)
  236.                 /* returns: error - value returned by
  237.                             get_abspathname */
  238.     char           *imgfile;
  239.                  /* input: string of size MAXNAMELEN - 1
  240.                    returns: (no error) resolved image filename
  241.                         (error) nothing of significance */
  242. {
  243.         /* get filename */
  244.     strcpy (imgfile, (char *) panel_get_value (image_board));
  245.     return (get_abspathname (imgfile, MAXNAMELEN, TYPE_FILE));
  246. }
  247. /* EOF */
  248. /* cat > src+obj/image/copy_line.c << "EOF" */
  249. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  250. /* copy_line: copy a line                */
  251. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  252. /* SCCS information: %W%    %G% - NCSA */
  253.  
  254. /* #include "image.h" */
  255.  
  256. copy_line (s1, s2, n)
  257.     char           *s1, *s2;
  258. {
  259.     register        i;
  260.  
  261.     for (i = 0; i < n; i++)
  262.         *s1++ = *s2++;
  263. }
  264. /* EOF */
  265. /* cat > src+obj/image/create_image_menu.c << "EOF" */
  266. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  267. /* create_image_menu: create menu for 'IMAGE'        */
  268. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  269. /* SCCS information: %W%    %G% - NCSA */
  270.  
  271. /* #include "image.h" */
  272.  
  273. create_image_menu ()
  274. {
  275.             /* submenu must be before menu! */
  276.     ani_sub_menu = menu_create (MENU_FONT, font_menu,
  277.                     MENU_STRINGS,
  278.                         "Regular size",
  279.                         "Zoom (2X)",
  280.                         0,
  281.                     MENU_NOTIFY_PROC, animate_proc,
  282.                     0);
  283.             /* menu creation */
  284.     image_menu = menu_create (MENU_ITEM,
  285.                       MENU_STRING, "Load",
  286.                       MENU_FONT, font_menu,
  287.                       MENU_NOTIFY_PROC, load_proc,
  288.                       0,
  289.                   MENU_ITEM,
  290.                       MENU_STRING, "Save",
  291.                       MENU_FONT, font_menu,
  292.                       MENU_ACTION_PROC, save_proc,
  293.                       0,
  294.                   MENU_ITEM,
  295.                       MENU_STRING, "Animate",
  296.                       MENU_FONT, font_menu,
  297.                       MENU_PULLRIGHT, ani_sub_menu,
  298.                       0,
  299.                   MENU_ITEM,
  300.                       MENU_STRING, "Stack",
  301.                       MENU_FONT, font_menu,
  302.                       MENU_ACTION_PROC, stack_proc,
  303.                       0,
  304.                   0);
  305.  
  306. }
  307. /* EOF */
  308. /* cat > src+obj/image/create_imagesave_box.c << "EOF"  */
  309. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  310. /* create_imagesave_box: create save image frame    */
  311. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  312. /* SCCS information: %W%    %G% - NCSA */
  313.  
  314. /* #include "option.h" */
  315.  
  316. create_imagesave_box ()
  317. {
  318.     int             h, w;
  319.  
  320.     imagesave_box = window_create (0, FRAME,
  321.                  FRAME_SHOW_LABEL, FALSE,
  322.                  WIN_SHOW, FALSE,
  323.                  0);
  324.     imagesave_panel = window_create (imagesave_box, PANEL,
  325.                    PANEL_FONT, font_panel,
  326.                    PANEL_LABEL_BOLD, TRUE,
  327.                    0);
  328.     panel_create_item (imagesave_panel, PANEL_MESSAGE,
  329.                PANEL_LABEL_STRING, "Please enter image filename -",
  330.                0);
  331.     imagesave_item = panel_create_item (imagesave_panel, PANEL_TEXT,
  332.                     PANEL_VALUE_DISPLAY_LENGTH, 64,
  333.                     PANEL_VALUE_STORED_LENGTH, MAXNAMELEN - 1,
  334.                     PANEL_LABEL_STRING, "     Filename: ",
  335.                     /*
  336.                     PANEL_MENU_CHOICE_STRINGS,
  337.                         "^N - Get directory name",
  338.                         0,
  339.                     PANEL_MENU_CHOICE_FONTS,
  340.                         font_menu,
  341.                         0,
  342.                     PANEL_MENU_CHOICE_VALUES,
  343.                         CTRL_N,
  344.                         0,
  345.                     PANEL_SHOW_MENU, TRUE,
  346.                     PANEL_NOTIFY_LEVEL, PANEL_ALL,
  347.                     PANEL_NOTIFY_PROC, defaults_proc,
  348.                     PANEL_CLIENT_DATA, 1,
  349.                     */
  350.                     0);
  351.     imagesave_toggle = panel_create_item (imagesave_panel, PANEL_CYCLE,
  352.                      PANEL_LABEL_STRING, "Save:",
  353.                      PANEL_CHOICE_STRINGS,
  354.                         "Image and palette (HDF)",
  355.                         "Compressed image and palette (HDF)",
  356.                          "Image (Raw)",
  357.                          0,
  358.                      0);
  359.     panel_create_item (imagesave_panel, PANEL_BUTTON,
  360.                PANEL_LABEL_IMAGE, panel_button_image (imagesave_panel, "Ok", 3, font_panel_button),
  361.                PANEL_CLIENT_DATA, 1,
  362.                PANEL_NOTIFY_PROC, imagesave_ok_cancel,
  363.                0);
  364.     panel_create_item (imagesave_panel, PANEL_BUTTON,
  365.                PANEL_LABEL_IMAGE, panel_button_image (imagesave_panel, "Cancel", 6, font_panel_button),
  366.                PANEL_CLIENT_DATA, 0,
  367.                PANEL_NOTIFY_PROC, imagesave_ok_cancel,
  368.                0);
  369.     window_fit (imagesave_panel);
  370.     window_fit (imagesave_box);
  371.  
  372.     w = (int) window_get (imagesave_box, WIN_WIDTH);
  373.     h = (int) window_get (imagesave_box, WIN_HEIGHT);
  374.     window_set (imagesave_box, WIN_X, 1150 / 2 - w / 2, WIN_Y, 900 / 2 - h / 2, 0);
  375.  
  376.         /* set color map same with imagetool */
  377.     (void) color_def_win ();
  378.  
  379. }
  380. /* EOF */
  381. /* cat > src+obj/image/get_imagename.c << "EOF" */
  382. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  383. /* get_imagename: get name of image            */
  384. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  385. /* SCCS information: %W%    %G% - NCSA */
  386.  
  387. /* #include "image.h" */
  388.  
  389. char **
  390. get_imagename ()
  391.                 /* returns: pointer to list of filesnames */
  392. {
  393.     char        fname[MAXNAMELEN];
  394.     char          **lfn;
  395.     int             err;
  396.  
  397.         /* get filename expression and expand */
  398.     strcpy (fname, (char *) panel_get_value (image_board));
  399.     if (strlen (fname) == 0)
  400.     {
  401.         msg_write ("Error: No image filename(s).");
  402.         return (NULL);
  403.     }
  404.     lfn = wildcard (fname, &err);
  405. /*
  406.    FIX: Does wildcard handle the case when the pathname is absolute or
  407.     relative with ..?
  408. */
  409.     switch (err)
  410.     {
  411.         case 0:        /* No error */
  412.             break;
  413.         case 1:        /* Syntax error */
  414.             msg_write ("Error: Syntax error in image filename(s).");
  415.             return (NULL);
  416.         case 2:        /* Memory allocation error */
  417.             msg_write ("Error: Not enough memory to process image filename(s).");
  418.             return (NULL);
  419.     }
  420.     if (lfn[0] == NULL)
  421.     {
  422.         msg_write ("Error: Image file(s) do not exist.");
  423.         return (NULL);
  424.     }
  425.     return (lfn);
  426. }
  427. /* EOF */
  428. /* cat > src+obj/image/get_imagesize.c << "EOF" */
  429. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  430. /* get_imagesize: get dimensions for raw image.        */
  431. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  432. /* SCCS information: %W%    %G% - NCSA */
  433.  
  434. /* #include "image.h" */
  435.  
  436. int
  437. get_imagesize (px, py)
  438.                 /* returns: 0 - success
  439.                         1 - bad dimensions */
  440.     int            *px, *py;
  441.                 /* returns: on success the pointers to the x
  442.                         and y dimensions */
  443. {
  444.     char           *ptr;
  445.  
  446.         /* get and check dimensions */
  447.     *px = strtol ((char *) panel_get_value (xdim_item), &ptr, 10);
  448.     if (*ptr != NULL || *px <= 0)
  449.         return (1);
  450.     *py = strtol ((char *) panel_get_value (ydim_item), &ptr, 10);
  451.     if (*ptr != NULL || *py <= 0)
  452.         return (1);
  453.     return (0);
  454. }
  455. /* EOF */
  456. /* cat > src+obj/image/image_board_proc.c << "EOF" */
  457. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  458. /* image_board_proc: handler called on text input    */
  459. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  460. /* SCCS information: %W%    %G% - NCSA */
  461.  
  462. /* #include "image.h" */
  463.  
  464. Panel_setting
  465. image_board_proc (item, event)
  466.     Panel_item      item;
  467.     Event          *event;
  468. {
  469.     switch (event_id (event))
  470.     {
  471.         case CTRL_N:
  472.             (void) display_pathnamesw ("Get image filename", update_image_board);
  473.             return PANEL_NONE;
  474.         case CTRL_L:
  475.             load_image ();
  476.             return PANEL_NONE;
  477.         case CTRL_A:
  478.             animation (0);
  479.             return PANEL_NONE;
  480.         case CTRL_C:
  481.             clear (item, event);
  482.             return PANEL_NONE;
  483.         default:
  484.             return panel_text_notify (item, event);
  485.     }
  486. }
  487. /* EOF */
  488. /* cat > src+obj/image/image_handler.c << "EOF" */
  489. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  490. /* image_handler: handler for image menu        */
  491. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  492. /* SCCS information: %W%    %G% - NCSA */
  493.  
  494. /* #include "image.h" */
  495.  
  496. image_handler (item, event)
  497.     Panel_item      item;
  498.     Event          *event;
  499. {
  500.         /* display image menu */
  501.     if (display_panel_menu (item, event, menu_panel, image_menu))
  502.         load_image ();
  503. }
  504. /* EOF */
  505. /* cat > src+obj/image/image_open.c << "EOF" */
  506. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  507. /* image_open: open image                */
  508. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  509. /* SCCS information: %W%    %G% - NCSA */
  510.  
  511. /* #include "image.h" */
  512.  
  513. image_open (fname)
  514.     char           *fname;
  515. {
  516.     return (open (fname, O_RDONLY));
  517. }
  518. /* EOF */
  519. /* cat > src+obj/save/imagesave_ok_cancel.c << "EOF" */
  520. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  521. /* imagesave_ok_cancel: ok/cancel notify proc for     */
  522. /*            saving image            */
  523. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  524. /* SCCS information: %W%    %G% - NCSA */
  525.  
  526. /* #include "image.h" */
  527.  
  528. imagesave_ok_cancel (item, event)
  529.     Panel_item      item;
  530.     Event          *event;
  531. {
  532.     int             which_item, nw, f;
  533.     int             value;
  534.     char            image[MAXNAMELEN];
  535.     struct pixrect *pr;
  536.     struct mpr_data *mpr;
  537.     int        xdim, ydim, xpos, ypos;
  538.     char        str[20];
  539.  
  540.     if ((which_item = (int) panel_get (item, PANEL_CLIENT_DATA)))
  541.     {
  542.         strcpy (image, (char *) panel_get_value (imagesave_item));
  543.  
  544.             /* check value  - report first error and wait for the
  545.                       user to respond again */
  546.  
  547.         if ((nw = word_count (image)) == 0)
  548.         {        /* null entry */
  549.             msg_write ("Error: No filename. Please provide one or cancel.");
  550.             return;
  551.         }
  552.         else if (nw != 1)
  553.         {
  554.             msg_write ("Error: More than one filename. Please provide only one or cancel.");
  555.             return;
  556.         }
  557.         (void) strip_wspace (image);
  558.  
  559.             /* append size to filename if file will be saved as RAW */
  560.         value = (int) panel_get_value (imagesave_toggle);
  561.         if (value == 2)
  562.         {        /* Raw */
  563.             sprintf(str, ".%dx%d", xdim, ydim);
  564.             strcat(image, str);
  565.         }
  566.         if ((f = creat (image, 0644)) == -1)
  567.         {
  568.             msg_write ("Error: File creation failed. Bad path or access problem."); 
  569.             return; 
  570.         }
  571.  
  572.         xdim = curr_image.xdim;
  573.         ydim = curr_image.ydim;
  574.         xpos = curr_image.startx;
  575.         ypos = curr_image.starty;
  576.  
  577.             /* create memory */
  578.         if ((pr = mem_create (xdim, ydim, PIXDEPTH)) == NULL)
  579.         {
  580.             msg_write ("Error: Not enough memory to make copy of image to save.");
  581.                close(f);
  582.                return(-1);
  583.         }
  584.  
  585.             /* read image from canvas */
  586.         pw_read (pr, 0, 0, xdim, ydim, PIX_SRC, pw, xpos, ypos);
  587.         mpr = mpr_d(pr);
  588.         
  589.             /* reverse image if necessary */
  590.         if(rev_image)
  591.         {
  592.                if (reverse_image (mpr->md_image, xdim, ydim))
  593.             {
  594.                 msg_write ("Error: Not enought memory to reverse image.");
  595.                    pr_close(pr);
  596.                       close(f);
  597.                    return(-1);
  598.                }
  599.         }
  600.             /* f is closed in save_image */
  601.         if (save_image (f, image, xdim, ydim, mpr->md_image, value))
  602.         {
  603.                 pr_close (pr);
  604.                 return;
  605.         }
  606.         window_set (imagesave_box, WIN_SHOW, FALSE, 0);
  607.         msg_write ("Note: Image saved.");
  608.     }
  609.     else
  610.         window_set (imagesave_box, WIN_SHOW, FALSE, 0);
  611.  
  612.     return;
  613. }
  614. /* EOF */
  615. /* cat > src+obj/image/load_RAWI.c << "EOF" */
  616. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  617. /* load_RAWI.c: routine to load and display a raw    */
  618. /*        image.                    */
  619. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  620. /* SCCS information: %W%    %G% - NCSA */
  621.  
  622. /* #include "image.h" */
  623.  
  624. int
  625. load_RAWI (fn, dim_x, dim_y, state)
  626.                 /* returns: error - 0 = no error
  627.                            -1 = error in loading
  628.                                 or displaying */
  629.     char           *fn;
  630.                 /* input: filename with raw image */
  631.     int             dim_x, dim_y;
  632.                 /* input: x and y dimensions */
  633.     int        state;
  634.                 /* input: state - SINGLE_IMAGE = single image
  635.                           ANIMATION    = animation
  636.                           FTP_SEQUENCE = ftp sequencing */
  637. {
  638.     struct pixrect *pr, *ptr;
  639.     struct mpr_data *mpr;
  640.     int             f;
  641.     int             nchar;
  642.  
  643.         /* create memory for image */
  644.     if ((pr = mem_create (dim_x, dim_y, PIXDEPTH)) == NULL)
  645.     {
  646.         msg_write ("Error: Not enough memory to load image.");
  647.         return (-1);
  648.     }
  649.  
  650.         /* read image from disk */
  651.     mpr = mpr_d (pr);
  652.     if ((f = image_open (fn)) == -1)
  653.     {
  654.         msg_write ("Error: Could not open image file to read.");
  655.         pr_close (pr);
  656.         return (-1);
  657.     }
  658.     if ((nchar = read (f, (char *) mpr->md_image, dim_x * dim_y)) == -1)
  659.     {
  660.         msg_write ("Error: Could not read image file.");
  661.         pr_close (pr);
  662.         return (-1);
  663.     }
  664.     close (f);
  665.     if (nchar != dim_x * dim_y)
  666.     {
  667.         sprintf (wkstr, "Error: Only %d characters of image file read. Should have been %d.", nchar, dim_x * dim_y);
  668.         msg_write (wkstr);
  669.         pr_close (pr);
  670.         return (-1);
  671.     }
  672.  
  673.         /* pad image */
  674.     pad_image (dim_x, dim_y, pr);
  675.  
  676.         /* reverse image if necessary */
  677.     if (rev_image)
  678.     {
  679.         if (reverse_image (mpr->md_image, dim_x, dim_y))
  680.         {
  681.             msg_write ("Error: Not enough memory to reverse image.");
  682.             pr_close (pr);
  683.             return (-1);
  684.         }
  685.     }
  686.  
  687.     switch (state)
  688.     {
  689.         case SINGLE_IMAGE:
  690.                 /* write image to canvas, release memory */
  691.             pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, pr, 0, 0);
  692.             pr_close (pr);
  693.             break;
  694.         case ANIMATION:
  695.             if (curr_movie.expanded)
  696.             {
  697.                 if ((image[curr_movie.nth_image] = expand2 (pr, dim_x, dim_y, 2)) == NULL)
  698.                 {
  699.                     pr_close (pr);
  700.                     return (-1);
  701.                 }
  702.                 else
  703.                 {        /* write expanded image to canvas, release unexpanded memory */
  704.                     pw_write (pw, startx, starty, 2 * dim_x, 2 * dim_y, PIX_SRC, image[curr_movie.nth_image], 0, 0);
  705.                     pr_close (pr);
  706.                 }
  707.             }
  708.             else
  709.             {        /* write image to canvas */
  710.                 image[curr_movie.nth_image] = pr;
  711.                 pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, image[curr_movie.nth_image], 0, 0);
  712.             }
  713.             break;
  714.         case FTP_SEQUENCE:
  715.             if (loop_zoomed)
  716.             {
  717.                 if ((ptr = expand2 (pr, dim_x, dim_y, 2)) == NULL)
  718.                 {
  719.                     pr_close (pr);
  720.                     return (-1);
  721.                 }
  722.                 else
  723.                 {        /* write expanded image to canvas, release memory */
  724.                     pw_write (pw, startx, starty, 2 * dim_x, 2 * dim_y, PIX_SRC, ptr, 0, 0);
  725.                     pr_close (ptr);
  726.                     pr_close (pr);
  727.                 }
  728.             }
  729.             else
  730.             {        /* write image to canvas, release memory */
  731.                 pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, pr, 0, 0);
  732.                 pr_close (pr);
  733.             }
  734.             break;
  735.     }
  736.  
  737.         /* save important info */
  738.     switch (state)
  739.     {
  740.         case SINGLE_IMAGE:
  741.             strcpy (curr_image.img_name, fn);
  742.             curr_image.itype = RAWI;
  743.             curr_image.img_num = 0;
  744.             strcpy (curr_image.pal_name, curr_pal.fn);
  745.             curr_image.ptype = curr_pal.type;
  746.             curr_image.pal_num = curr_pal.num;
  747.             curr_image.startx = startx;
  748.             curr_image.starty = starty;
  749.             curr_image.org_xdim = dim_x;
  750.             curr_image.org_ydim = dim_y;
  751.             
  752.             curr_image.xdim = dim_x;
  753.             curr_image.ydim = dim_y;
  754.             curr_image.zoomx = 1;
  755.             curr_image.zoomy = 1;
  756.             curr_image.expanded = 0;
  757.             
  758.             curr_image.cutout = 0;
  759.             curr_image.nth_image = 0;
  760.             curr_image.image = NULL;
  761.  
  762.             return (0);
  763.         case ANIMATION:
  764.             return (0);
  765.         case FTP_SEQUENCE:
  766.             strcpy (curr_image.img_name, fn);
  767.             curr_image.itype = RAWI;
  768.             curr_image.img_num = 0;
  769.             strcpy (curr_image.pal_name, curr_pal.fn);
  770.             curr_image.ptype = curr_pal.type;
  771.             curr_image.pal_num = curr_pal.num;
  772.             curr_image.startx = startx;
  773.             curr_image.starty = starty;
  774.             curr_image.org_xdim = dim_x;
  775.             curr_image.org_ydim = dim_y;
  776.         
  777.             if(loop_zoomed)
  778.             {
  779.                     curr_image.xdim = dim_x * 2;
  780.                 curr_image.ydim = dim_y * 2;
  781.                     curr_image.zoomx = 2;
  782.                 curr_image.zoomy = 2;
  783.                      curr_image.expanded = 1;
  784.             }
  785.             else
  786.             {
  787.                     curr_image.xdim = dim_x;
  788.                 curr_image.ydim = dim_y;
  789.                     curr_image.zoomx = 1;
  790.                 curr_image.zoomy = 1;
  791.                     curr_image.expanded = 0;
  792.             }
  793.         
  794.             curr_image.cutout = 0;
  795.             curr_image.nth_image = 0;
  796.             curr_image.image = NULL;
  797.  
  798.             return (0);
  799.     }
  800. }
  801. /* EOF */
  802. /* cat > src+obj/image/load_RIS8.c << "EOF" */
  803. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  804. /* load_RIS8.c: routine to load and display a 8-Bit    */
  805. /*        raster image set.            */
  806. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  807. /* SCCS information: %W%    %G% - NCSA */
  808.  
  809. /* #include "image.h" */
  810.  
  811. int
  812. load_RIS8 (fn, dim_x, dim_y, ref, load_pal, state)
  813.                 /* returns error - 0 = no error
  814.                           -1 = error in loading
  815.                                or displaying */
  816.     char           *fn;
  817.                 /* input: filename with RIS8 image */
  818.     int             dim_x, dim_y;
  819.                 /* input: x and y dimensions */
  820.     uint16          ref;
  821.                 /* input: reference number of RIS8 rig */
  822.     bool            load_pal;
  823.                 /* input: FALSE - ignore palette
  824.                       TRUE  - attempt to load palette */
  825.     int        state;
  826.                 /* input: state - SINGLE_IMAGE = single image
  827.                           ANIMATION    = animation
  828.                           FTP_SEQUENCE = ftp sequencing */
  829. {
  830.     struct pixrect *pr, *ptr;
  831.     struct mpr_data *mpr;
  832.     int             f;
  833.     int             n;
  834.     unsigned char   palette[PALETTE_SIZE];
  835.  
  836.         /* create memory for image */
  837.     if ((pr = mem_create (dim_x, dim_y, PIXDEPTH)) == NULL)
  838.     {
  839.         msg_write ("Error: Not enough memory to load image.");
  840.         return (-1);
  841.     }
  842.  
  843.         /* read image from disk */
  844.     mpr = mpr_d (pr);
  845.     if (DFR8readref (fn, ref))
  846.     {
  847.         sprintf (wkstr, "HDF error (DFerror = %d): While trying to locate the RIS8 in the file.", DFerror);
  848.         msg_write (wkstr);
  849.         return (-1);
  850.     }
  851.     if (load_pal)
  852.     {
  853.         if (DFR8getimage (fn, (char *) mpr->md_image, dim_x, dim_y, palette))
  854.         {
  855.             sprintf (wkstr, "HDF error (DFerror = %d): While trying to read the RIS8 in the file.", DFerror);
  856.             msg_write (wkstr);
  857.             return (-1);
  858.         }
  859.             /* no checking done */
  860.         (void) set_palHDF (fn, PAL_RIS8, palette, REGULAR);
  861.     }
  862.     else
  863.     {
  864.         if (DFR8getimage (fn, (char *) mpr->md_image, dim_x, dim_y, NULL))
  865.         {
  866.             sprintf (wkstr, "HDF error (DFerror = %d): While trying to read the RIS8 in the file.", DFerror);
  867.             msg_write (wkstr);
  868.             return (-1);
  869.         }
  870.     }
  871.  
  872.         /* pad image */
  873.     pad_image (dim_x, dim_y, pr);
  874.  
  875.         /* reverse image if necessary */
  876.     if (rev_image)
  877.     {
  878.         if (reverse_image (mpr->md_image, dim_x, dim_y))
  879.         {
  880.             msg_write ("Error: Not enough memory to reverse image.");
  881.             pr_close (pr);
  882.             return (-1);
  883.         }
  884.     }
  885.  
  886.     switch (state)
  887.     {
  888.         case SINGLE_IMAGE:
  889.                 /* write image to canvas, release memory */
  890.             pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, pr, 0, 0);
  891.             pr_close (pr);
  892.             break;
  893.         case ANIMATION:
  894.             if (curr_movie.expanded)
  895.             {
  896.                 if ((image[curr_movie.nth_image] = expand2 (pr, dim_x, dim_y, 2)) == NULL)
  897.                 {
  898.                     pr_close (pr);
  899.                     return (-1);
  900.                 }
  901.                 else
  902.                 {        /* write expanded image to canvas, release unexpanded memory */
  903.                     pw_write (pw, startx, starty, 2 * dim_x, 2 * dim_y, PIX_SRC, image[curr_movie.nth_image], 0, 0);
  904.                     pr_close (pr);
  905.                 }
  906.             }
  907.             else
  908.             {        /* write image to canvas */
  909.                 image[curr_movie.nth_image] = pr;
  910.                 pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, image[curr_movie.nth_image], 0, 0);
  911.             }
  912.             break;
  913.         case FTP_SEQUENCE:
  914.             if (loop_zoomed)
  915.             {
  916.                 if ((ptr = expand2 (pr, dim_x, dim_y, 2)) == NULL)
  917.                 {
  918.                     pr_close (pr);
  919.                     return (-1);
  920.                 }
  921.                 else
  922.                 {        /* write expanded image to canvas, release memory */
  923.                     pw_write (pw, startx, starty, 2 * dim_x, 2 * dim_y, PIX_SRC, ptr, 0, 0);
  924.                     pr_close (ptr);
  925.                     pr_close (pr);
  926.                 }
  927.             }
  928.             else
  929.             {        /* write image to canvas, release memory */
  930.                 pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, pr, 0, 0);
  931.                 pr_close (pr);
  932.             }
  933.             break;
  934.     }
  935.  
  936.         /* put out dimensions */
  937.     put_imagesize (dim_x, dim_y);
  938.  
  939.         /* save important info */
  940.     switch (state)
  941.     {
  942.         case SINGLE_IMAGE:
  943.             strcpy (curr_image.img_name, fn);
  944.             curr_image.itype = RIS8;
  945.             curr_image.img_num = 1;
  946.             strcpy (curr_image.pal_name, curr_pal.fn);
  947.             curr_image.ptype = curr_pal.type;
  948.             curr_image.pal_num = curr_pal.num;
  949.             curr_image.startx = startx;
  950.             curr_image.starty = starty;
  951.             curr_image.org_xdim = dim_x;
  952.             curr_image.org_ydim = dim_y;
  953.             
  954.             curr_image.xdim = dim_x;
  955.             curr_image.ydim = dim_y;
  956.             curr_image.zoomx = 1;
  957.             curr_image.zoomy = 1;
  958.             curr_image.expanded = 0;
  959.             
  960.             curr_image.cutout = 0;
  961.             curr_image.nth_image = 0;
  962.             curr_image.image = NULL;
  963.  
  964.             return (0);
  965.         case ANIMATION:
  966.             return (0);
  967.         case FTP_SEQUENCE:
  968.             strcpy (curr_image.img_name, fn);
  969.             curr_image.itype = RIS8;
  970.             curr_image.img_num = 1;
  971.             strcpy (curr_image.pal_name, curr_pal.fn);
  972.             curr_image.ptype = curr_pal.type;
  973.             curr_image.pal_num = curr_pal.num;
  974.             curr_image.startx = startx;
  975.             curr_image.starty = starty;
  976.             curr_image.org_xdim = dim_x;
  977.             curr_image.org_ydim = dim_y;
  978.         
  979.             if(loop_zoomed)
  980.             {
  981.                     curr_image.xdim = dim_x * 2;
  982.                 curr_image.ydim = dim_y * 2;
  983.                     curr_image.zoomx = 2;
  984.                 curr_image.zoomy = 2;
  985.                      curr_image.expanded = 1;
  986.             }
  987.             else
  988.             {
  989.                     curr_image.xdim = dim_x;
  990.                 curr_image.ydim = dim_y;
  991.                     curr_image.zoomx = 1;
  992.                 curr_image.zoomy = 1;
  993.                     curr_image.expanded = 0;
  994.             }
  995.         
  996.             curr_image.cutout = 0;
  997.             curr_image.nth_image = 0;
  998.             curr_image.image = NULL;
  999.  
  1000.             return (0);
  1001.     }
  1002. }
  1003. /* EOF */
  1004. /* cat > src+obj/image/load_SDS2D.c << "EOF" */
  1005. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1006. /* load_SDS2D.c: routine to load and display a 2-D    */
  1007. /*         Scientific Data Set image.        */
  1008. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1009. /* SCCS information: %W%    %G% - NCSA */
  1010.  
  1011. /* #include "image.h" */
  1012.  
  1013. int
  1014. load_SDS2D (fn, dim_x, dim_y, state)
  1015.                 /* returns error - 0 = no error
  1016.                           -1 = error in loading
  1017.                                or displaying */
  1018.     char           *fn;
  1019.                 /* input: filename with raw image */
  1020.     int             dim_x, dim_y;
  1021.                 /* input: x and y dimensions */
  1022.     int        state;
  1023.                 /* input: state - SINGLE_IMAGE = single image
  1024.                           ANIMATION    = animation
  1025.                           FTP_SEQUENCE = ftp sequencing */
  1026. {
  1027.     struct pixrect *pr, *ptr;
  1028.     struct mpr_data *mpr;
  1029.     float          *fmem;
  1030.     int             rank;
  1031.     int             dims[MAXSDSRANK];
  1032.  
  1033.         /* create memory for data and image */
  1034.     if ((fmem = (float *) malloc (dim_x * dim_y * sizeof (float))) == NULL)
  1035.     {
  1036.         msg_write ("Error: Not enough memory to load 2-D data set.");
  1037.         return (-1);
  1038.     }
  1039.     if ((pr = mem_create (dim_x, dim_y, PIXDEPTH)) == NULL)
  1040.     {
  1041.         msg_write ("Error: Not enough memory to make scaled image.");
  1042.         free ((char *) fmem);
  1043.         return (-1);
  1044.     }
  1045.  
  1046.         /* read image from disk */
  1047.     DFSDrestart();    
  1048.     while (TRUE)
  1049.     {
  1050.         if (DFSDgetdims (fn, &rank, dims, MAXSDSRANK))
  1051.         {
  1052.             switch (DFerror)
  1053.             {
  1054.                 case DFE_NOTENOUGH:    /* rank too big */
  1055.                     break;
  1056.                 default:
  1057.                     sprintf (wkstr, "Internal Error (DFerror = %d): While trying to load 2-D Scientific Data Set.", DFerror);
  1058.                     msg_write (wkstr);
  1059.                     free ((char *) fmem);
  1060.                     pr_close (pr);
  1061.                     return (-1);
  1062.             }
  1063.         }
  1064.         else if (rank == 2)    /* 2D dataset found */
  1065.         {
  1066.             if (dims[0] != dim_x || dims[1] != dim_y)
  1067.             {
  1068.                 sprintf (wkstr, "Internal Error: Inconsistent dimensions - (x, y): incoming (%d, %d) - found (%d, %d).", dim_x, dim_y, dims[0], dims[1]);
  1069.                 msg_write (wkstr);
  1070.                 free ((char *) fmem);
  1071.                 pr_close (pr);
  1072.                 return (-1);
  1073.             }
  1074.             else if (DFSDgetdata (fn, rank, dims, fmem))
  1075.             {
  1076.                 sprintf (wkstr, "Internal Error (DFerror = %d): While trying to load 2-D Scientific Data Set.", DFerror);
  1077.                 msg_write (wkstr);
  1078.                 free ((char *) fmem);
  1079.                 pr_close (pr);
  1080.                 return (-1);
  1081.             }
  1082.             break;
  1083.         }
  1084.     }
  1085.  
  1086.         /* supply a warning about dialog box - if one is to appear */
  1087.      if (((int) panel_get_value (SDS_toggle)) == 0)
  1088.         msg_write ("Note: Please wait for dialog box to scale image.");
  1089.  
  1090.         /* scale image */
  1091.     if (image_scale (dim_x, dim_y, fmem, pr))
  1092.     {
  1093.         free ((char *) fmem);
  1094.         pr_close (pr);
  1095.         return (-1);
  1096.     }
  1097.     free ((char *) fmem);
  1098.  
  1099.         /* pad image */
  1100.     pad_image (dim_x, dim_y, pr);
  1101.  
  1102.         /* reverse image if necessary */
  1103.     mpr = mpr_d (pr);
  1104.     if (rev_image)
  1105.     {
  1106.         if (reverse_image (mpr->md_image, dim_x, dim_y))
  1107.         {
  1108.             msg_write ("Error: Not enough memory to reverse image.");
  1109.             pr_close (pr);
  1110.             return (-1);
  1111.         }
  1112.     }
  1113.  
  1114.     switch (state)
  1115.     {
  1116.         case SINGLE_IMAGE:
  1117.                 /* write image to canvas, release memory */
  1118.             pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, pr, 0, 0);
  1119.             pr_close (pr);
  1120.             break;
  1121.         case ANIMATION:
  1122.             if (curr_movie.expanded)
  1123.             {
  1124.                 if ((image[curr_movie.nth_image] = expand2 (pr, dim_x, dim_y, 2)) == NULL)
  1125.                 {
  1126.                     pr_close (pr);
  1127.                     return (-1);
  1128.                 }
  1129.                 else
  1130.                 {        /* write expanded image to canvas, release unexpanded memory */
  1131.                     pw_write (pw, startx, starty, 2 * dim_x, 2 * dim_y, PIX_SRC, image[curr_movie.nth_image], 0, 0);
  1132.                     pr_close (pr);
  1133.                 }
  1134.             }
  1135.             else
  1136.             {        /* write image to canvas */
  1137.                 image[curr_movie.nth_image] = pr;
  1138.                 pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, image[curr_movie.nth_image], 0, 0);
  1139.             }
  1140.             break;
  1141.         case FTP_SEQUENCE:
  1142.             if (loop_zoomed)
  1143.             {
  1144.                 if ((ptr = expand2 (pr, dim_x, dim_y, 2)) == NULL)
  1145.                 {
  1146.                     pr_close (pr);
  1147.                     return (-1);
  1148.                 }
  1149.                 else
  1150.                 {        /* write expanded image to canvas, release memory */
  1151.                     pw_write (pw, startx, starty, 2 * dim_x, 2 * dim_y, PIX_SRC, ptr, 0, 0);
  1152.                     pr_close (ptr);
  1153.                     pr_close (pr);
  1154.                 }
  1155.             }
  1156.             else
  1157.             {        /* write image to canvas, release memory */
  1158.                 pw_write (pw, startx, starty, dim_x, dim_y, PIX_SRC, pr, 0, 0);
  1159.                 pr_close (pr);
  1160.             }
  1161.             break;
  1162.     }
  1163.  
  1164.         /* put out dimensions */
  1165.     put_imagesize (dim_x, dim_y);
  1166.  
  1167.         /* save important info */
  1168.     switch (state)
  1169.     {
  1170.         case SINGLE_IMAGE:
  1171.             strcpy (curr_image.img_name, fn);
  1172.             curr_image.itype = SDS2D;
  1173.             curr_image.img_num = 1;
  1174.             strcpy (curr_image.pal_name, curr_pal.fn);
  1175.             curr_image.ptype = curr_pal.type;
  1176.             curr_image.pal_num = curr_pal.num;
  1177.             curr_image.startx = startx;
  1178.             curr_image.starty = starty;
  1179.             curr_image.org_xdim = dim_x;
  1180.             curr_image.org_ydim = dim_y;
  1181.             
  1182.             curr_image.xdim = dim_x;
  1183.             curr_image.ydim = dim_y;
  1184.             curr_image.zoomx = 1;
  1185.             curr_image.zoomy = 1;
  1186.             curr_image.expanded = 0;
  1187.             
  1188.             curr_image.cutout = 0;
  1189.             curr_image.nth_image = 0;
  1190.             curr_image.image = NULL;
  1191.  
  1192.             return (0);
  1193.         case ANIMATION:
  1194.             return (0);
  1195.         case FTP_SEQUENCE:
  1196.             strcpy (curr_image.img_name, fn);
  1197.             curr_image.itype = SDS2D;
  1198.             curr_image.img_num = 1;
  1199.             strcpy (curr_image.pal_name, curr_pal.fn);
  1200.             curr_image.ptype = curr_pal.type;
  1201.             curr_image.pal_num = curr_pal.num;
  1202.             curr_image.startx = startx;
  1203.             curr_image.starty = starty;
  1204.             curr_image.org_xdim = dim_x;
  1205.             curr_image.org_ydim = dim_y;
  1206.         
  1207.             if(loop_zoomed)
  1208.             {
  1209.                     curr_image.xdim = dim_x * 2;
  1210.                 curr_image.ydim = dim_y * 2;
  1211.                     curr_image.zoomx = 2;
  1212.                 curr_image.zoomy = 2;
  1213.                      curr_image.expanded = 1;
  1214.             }
  1215.             else
  1216.             {
  1217.                     curr_image.xdim = dim_x;
  1218.                 curr_image.ydim = dim_y;
  1219.                     curr_image.zoomx = 1;
  1220.                 curr_image.zoomy = 1;
  1221.                     curr_image.expanded = 0;
  1222.             }
  1223.         
  1224.             curr_image.cutout = 0;
  1225.             curr_image.nth_image = 0;
  1226.             curr_image.image = NULL;
  1227.  
  1228.             return (0);
  1229.     }
  1230. }
  1231. /* EOF */
  1232. /* cat > src+obj/image/load_image.c << "EOF" */
  1233. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1234. /* load_image: main routine for loading an image.    */
  1235. /*           Should be same as load_proc        */
  1236. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1237. /* SCCS information: %W%    %G% - NCSA */
  1238.  
  1239. /* #include "image.h" */
  1240.  
  1241. int
  1242. load_image ()
  1243. {
  1244.     if (load_proc2 ((int) panel_get_value (load_toggle)))
  1245.         return;
  1246.  
  1247.     reset_pos ();
  1248.     line_drawn = square_drawn = 0;
  1249.     clear_request = 0;
  1250.     first_middle_down = 0;
  1251.     return (0);
  1252. }
  1253. /* EOF */
  1254. /* cat > src+obj/image/load_proc.c << "EOF" */
  1255. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1256. /* load_proc: notify procedure for load_image menu    */
  1257. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1258. /* SCCS information: %W%    %G% - NCSA */
  1259.  
  1260. /* #include "image.h" */
  1261.  
  1262. void
  1263. load_proc (m, mi)
  1264.     Menu            m;
  1265.     Menu_item       mi;
  1266. {
  1267.     if (load_proc2 ((int) panel_get_value (load_toggle)))
  1268.         return;
  1269.  
  1270.     reset_pos ();
  1271.     line_drawn = square_drawn = 0;
  1272.     clear_request = 0;
  1273.     first_middle_down = 0;
  1274.     return;
  1275. }
  1276. /* EOF */
  1277. /* cat > src+obj/image/load_proc2.c << "EOF" */
  1278. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1279. /* load_proc2: attempt to load image of desired type    */
  1280. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1281. /* SCCS information: %W%    %G% - NCSA */
  1282.  
  1283. /* #include "image.h" */
  1284.  
  1285. int
  1286. load_proc2 (index_type)
  1287.                 /* returns: 0 - success
  1288.                        -1 - error     */
  1289.     int             index_type;
  1290.                 /* input: index of what to load from load_toggle */
  1291. {
  1292.     enum desired_img_type in_type;
  1293.     enum img_type   out_type;
  1294.     int             err;
  1295.     char            filename[MAXNAMELEN];
  1296.     int             dim_x, dim_y;
  1297.     uint16          iref;
  1298.     bool            load_pal, load_pal_old;
  1299.  
  1300.     in_type = index_type % 3;
  1301.     load_pal = (index_type < 2) ? TRUE : FALSE;
  1302.  
  1303.     switch ((err = check_imgfn (filename)))
  1304.     {
  1305.         case 0:        /* no error */
  1306.             break;
  1307.         case 1:
  1308.             msg_write ("Error: Image file not found.");
  1309.             return (-1);
  1310.         case 2:
  1311.             msg_write ("Error: Image file not a file.");
  1312.             return (-1);
  1313.         case 3:
  1314.             msg_write ("Error: No image filename.");
  1315.             return (-1);
  1316.         case 4:
  1317.             msg_write ("Error: More than one image file given.");
  1318.             return (-1);
  1319.         case 5:
  1320.             msg_write ("Error: Wildcard matches more than one image file.");
  1321.             return (-1);
  1322.         case 6:
  1323.             msg_write ("Error: Wildcard syntax.");
  1324.             return (-1);
  1325.         default:
  1326.             sprintf (wkstr, "Internal error (%d): While checking image filename.", err);
  1327.             msg_write (wkstr);
  1328.             return (-1);
  1329.     }
  1330. /* printf */
  1331. /* msg_write ("just before check_image"); */
  1332.  
  1333.     load_pal_old = load_pal;
  1334.     switch ((err = check_image (in_type, filename, &out_type, &dim_x, &dim_y, &iref, &load_pal)))
  1335.     {
  1336.         case 0:        /* no error */
  1337.             break;
  1338.         case 1:
  1339.             msg_write ("Error: Image file does not exist.");
  1340.             return (-1);
  1341.         case 2:
  1342.             msg_write ("Error: Dimension(s) are bad.");
  1343.             return (-1);
  1344.         case 3:
  1345.             msg_write ("Error: Size of image doesn't match dimensions.");
  1346.             return (-1);
  1347.         case 4:
  1348.             msg_write ("Error: No 8-bit Raster Image Set found in image file.");
  1349.             return (-1);
  1350.         case 5:
  1351.             msg_write ("Error: No 2D Scientific Data Set found in image file.");
  1352.             return (-1);
  1353.         case 6:
  1354.             msg_write ("Error: No 8-bit Raster Image Set or 2D Scientific Data Set found in image file.");
  1355.             return (-1);
  1356.         case 7:
  1357.             sprintf (wkstr, "Error: Image too big!. (x, y) size = (%d, %d) not in range (%d, %d)", dim_x, dim_y, XMAX_IMAGE, YMAX_IMAGE);
  1358.             msg_write (wkstr);
  1359.             return (-1);
  1360.         default:
  1361.             sprintf (wkstr, "HDF file error (DFerror = %d): While checking image file.", err);
  1362.             msg_write (wkstr);
  1363.             return (-1);
  1364.     }
  1365.     if (out_type == RIS8 && (load_pal != load_pal_old))
  1366.         msg_write ("Warning: No palette with 8-bit Raster Image Set in image file.");
  1367.  
  1368. /* printf */
  1369. /* sprintf (wkstr, "load_proc2: %d, %d, %d, %d", dim_x, dim_y, iref, load_pal);
  1370. msg_write(wkstr); */
  1371.  
  1372.     switch (out_type)
  1373.     {
  1374.         case RAWI:
  1375.             if (load_RAWI (filename, dim_x, dim_y, SINGLE_IMAGE))
  1376.                 return (-1);
  1377.             break;
  1378.         case RIS8:
  1379.             if (load_RIS8 (filename, dim_x, dim_y, iref, load_pal, SINGLE_IMAGE))
  1380.                 return (-1);
  1381.             break;
  1382.         case SDS2D:
  1383. /* printf */
  1384. /* msg_write ("load_proc2: before load_SDS2D"); */
  1385.             if (load_SDS2D (filename, dim_x, dim_y, SINGLE_IMAGE))
  1386.                 return (-1);
  1387.             break;
  1388.     }
  1389.     return (0);
  1390. }
  1391. /* EOF */
  1392. /* cat > src+obj/image/pad_image.c << "EOF" */
  1393. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1394. /* pad_image: expand and pad a memory pixrect with     */
  1395. /*          NULL.                    */
  1396. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1397. /* SCCS information: %W%    %G% - NCSA */
  1398.  
  1399. /* #include "image.h" */
  1400.  
  1401. void 
  1402. pad_image (xpts, ypts, mpr)
  1403.     int             xpts, ypts;
  1404.     struct pixrect *mpr;
  1405. {
  1406.     struct mpr_data *d;
  1407.     char           *image;
  1408.     register int    i, j, k, npad;
  1409.     register char  *p0, *p1;
  1410.  
  1411.     d = mpr_d (mpr);
  1412.     image = (char *) d->md_image;
  1413.     p0 = image + xpts * ypts - 1;
  1414.  
  1415.     npad = pixrect_pad (xpts);
  1416.     p1 = image + (xpts + npad) * ypts - 1;
  1417.  
  1418. /* printf ("xpts, npad = %d %d\n", xpts, npad); */
  1419.  
  1420.     if (npad == 0)    /* no padding required */
  1421.         return;
  1422.  
  1423.         /* make space and pad with NULL's */
  1424.     for (k = 0; k < npad; k++)
  1425.         *p1-- = NULL;
  1426.     for (i = 0; i < ypts - 1; i++)
  1427.     {        /* do all rows except first */
  1428.         for (j = 0; j < xpts; j++)
  1429.             *p1-- = *p0--;
  1430.         for (k = 0; k < npad; k++)
  1431.             *p1-- = NULL;
  1432.     }
  1433.     return;
  1434. }
  1435. /* EOF */
  1436. /* cat > src+obj/image/put_imagesize.c << "EOF" */
  1437. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1438. /* put_imagesize: puts out the dimensions on the panel  */
  1439. /*          for HDF files.            */
  1440. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1441. /* SCCS information: %W%    %G% - NCSA */
  1442.  
  1443. /* #include "image.h" */
  1444.  
  1445. int
  1446. put_imagesize (dim_x, dim_y)
  1447.                 /* returns: 0 - success (should not fail) */
  1448.     int            dim_x, dim_y;
  1449.                 /* input: dimensions of image or dataset */
  1450. {
  1451.  
  1452.         /* write dimensions to panel */
  1453.     sprintf (wkstr, "%d", dim_x);
  1454.     panel_set_value (xdim_item, wkstr);
  1455.     sprintf (wkstr, "%d", dim_y);
  1456.     panel_set_value (ydim_item, wkstr);
  1457.  
  1458.     return (0);
  1459. }
  1460. /* EOF */
  1461. /* cat > src+obj/image/reverse_image.c << "EOF" */
  1462. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1463. /* reverse_image: flip image about horizontal center    */
  1464. /*          line.                    */
  1465. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1466. /* SCCS information: %W%    %G% - NCSA */
  1467.  
  1468. /* #include "image.h" */
  1469.  
  1470. reverse_image (ptr, xdim, ydim)
  1471.                 /* returns: error - 0 = no error
  1472.                            -1 = error in allocating
  1473.                             temporary storage */
  1474.     char           *ptr;
  1475.     int             xdim, ydim;
  1476. {
  1477.     register char  *p;
  1478.     register int    i, j, ix, jy, npad;
  1479.  
  1480.     if ((p = (char *) malloc (xdim)) == NULL)
  1481.     {
  1482.         msg_write ("Error: Not enough memory when trying to flip image.");
  1483.         return (-1);
  1484.     }
  1485.  
  1486.     npad = pixrect_pad (xdim);
  1487.     xdim += npad;
  1488.  
  1489.     i = 0;
  1490.     j = ydim - 1;
  1491.     while (i < j)
  1492.     {
  1493.         ix = i * xdim;
  1494.         jy = j * xdim;
  1495.         copy_line (p, &ptr[jy], xdim);
  1496.         copy_line (&ptr[jy], &ptr[ix], xdim);
  1497.         copy_line (&ptr[ix], p, xdim);
  1498.         i++;
  1499.         j--;
  1500.     }
  1501.     free (p);
  1502.     return (0);
  1503. }
  1504. /* EOF */
  1505. /* cat > src+obj/image/save_image.c << "EOF" */
  1506. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1507. /* save_image: stores image alone in raw raster file    */
  1508. /*           or dimensions, image, and palette as a    */
  1509. /*           HDF 8-bit Raster Image set.        */
  1510. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1511. /* SCCS information: %W%    %G% - NCSA */
  1512.  
  1513. #include "all.h"
  1514. #include "newext.h"
  1515.  
  1516. int
  1517. save_image (f, fn, nx, ny, s, mode)
  1518.     int        f;    /* input: file descriptor to write to.
  1519.                       f is closed in this routine */
  1520.     char           *fn;    /* input: filename */
  1521.     int             nx, ny;    /* input: dimensions of image */
  1522.     unsigned char  *s;    /* input: image */
  1523.     int             mode;    /* input: 0 - HDF 8-bit Raster Image set
  1524.                           (uncompressed)
  1525.                           1 - HDF 8-bit Raster Image set
  1526.                           (compressed)
  1527.                       2 - image only in raw raster */
  1528. {
  1529.     int             i, nw;
  1530.     unsigned char  *p_palette;
  1531.     unsigned char    red[LUTSIZE], green[LUTSIZE], blue[LUTSIZE];
  1532.  
  1533.     if (mode == 0 || mode == 1)    /* HDF 8-bit Raster Image set */
  1534.     {
  1535.         close (f);
  1536.         p_palette = palette;
  1537.         pw_getcolormap (pw, 0, color_index, red, green, blue);
  1538.         for (i = 0; i < 256; i++)
  1539.         {
  1540.             *p_palette++ = red[i];
  1541.             *p_palette++ = green[i];
  1542.             *p_palette++ = blue[i];
  1543.         }
  1544.         if (DFR8setpalette (palette))
  1545.         {
  1546.             sprintf (wkstr, "Error: HDF setpalette call failed (DFerror = %d).", DFerror);
  1547.             msg_write (wkstr);
  1548.             return (-1);
  1549.         }
  1550.         if (DFR8putimage (fn, s, nx, ny, mode))
  1551.         {
  1552.             sprintf (wkstr, "Error: HDF putimage call failed (DFerror = %d).", DFerror);
  1553.             msg_write (wkstr);
  1554.             return (-1);
  1555.         }
  1556.     }
  1557.     else if (mode == 2)    /* raw raster */
  1558.     {
  1559.         if (nx * ny != (nw = write (f, image, nx * ny)))
  1560.         {
  1561.             if (nw == -1)
  1562.             {
  1563.                 msg_write ("Error: Raw raster write to file failed.");
  1564.                 close (f);
  1565.                 return (-1);
  1566.             }
  1567.             else
  1568.             {
  1569.                 sprintf (wkstr, "Error: Only %d of %d bytes written to raw raster file.", nw, nx * ny);
  1570.                 msg_write (wkstr);
  1571.                 close (f);
  1572.                 return (-1);
  1573.             }
  1574.         }
  1575.         close (f);
  1576.     }
  1577.     else
  1578.     {
  1579.         sprintf (wkstr, "Internal error: Bad mode value (Mode = %d).", mode);
  1580.         msg_write (wkstr);
  1581.         close (f);
  1582.         return (-1);
  1583.     }
  1584.     return (0);
  1585. }
  1586. /* EOF */
  1587. /* cat > src+obj/image/save_proc.c << "EOF" */
  1588. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1589. /* save_proc: save image to disk            */
  1590. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1591. /* SCCS information: %W%    %G% - NCSA */
  1592.  
  1593. /* #include "image.h" */
  1594.  
  1595. save_proc (m, mi)
  1596.     Menu            m;
  1597.     Menu_item       mi;
  1598. {
  1599.     window_set (imagesave_box, WIN_SHOW, TRUE, 0);
  1600. }
  1601. /* EOF */
  1602. /* cat > src+obj/image/size_ok.c << "EOF" */
  1603. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1604. /* size_ok: imagesize to big?
  1605. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1606. /* SCCS information: %W%    %G% - NCSA */
  1607.  
  1608. /* #include "image.h" */
  1609.  
  1610. int
  1611. size_ok (x, y)
  1612.     int             x, y;
  1613. {
  1614.     if (x > XMAX_IMAGE || y > YMAX_IMAGE)
  1615.         return (0);
  1616.     else
  1617.         return (1);
  1618. }
  1619. /* EOF */
  1620. /* cat > src+obj/image/update_image_board.c << "EOF" */
  1621. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1622. /* update_image_board: updates the image_board with     */
  1623. /*               filename from pathname frame    */
  1624. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1625. /* SCCS information: %W%    %G% - NCSA */
  1626.  
  1627. /* #include "main.h" */
  1628.  
  1629. int
  1630. update_image_board (dir, file)
  1631.     char           *dir;
  1632.     char           *file;
  1633. {
  1634.          /* put in code later to change the directory if it is
  1635.             different then that stored in dir_board */
  1636.     panel_set_value (image_board, file);
  1637. }
  1638. /* EOF */
  1639.